package zuosheng.day5;

/**
 * @author lj
 * @Description:
 * @date 2020-11-04 23:02
 * @QQ 851477108
 */


public class ZigZagPrintMatrix {
    public static void main(String[] args) {
        int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
        printMatrixZigZag(matrix);
    }
    public static void printMatrixZigZag(int[][] matrix) {
        int Ay = 0;
        int Ax = 0;
        int By = 0;
        int Bx = 0;
        int endy = matrix.length - 1;
        int endx = matrix[0].length - 1;
        boolean flag = false;
        while (Ay != endy + 1) {
            printLevel(matrix, Ay, Ax, By , Bx, flag);
            Ay = Ax == endx ? Ay + 1 : Ay;
            Ax = Ax == endx ? Ax : Ax + 1;
            Bx = By == endy ? Bx + 1 : Bx;
            By = By == endy ? By : By + 1;
            flag = !flag;
        }
        System.out.println();
    }

    public static void printLevel(int[][] m, int Ay, int Ax, int By, int Bx,
                                  boolean f) {
        if (f) {
            while (Ay != By + 1) {
                System.out.print(m[Ay++][Ax--] + " ");
            }
        } else {
            while (By != Ay - 1) {
                System.out.print(m[By--][Bx++] + " ");
            }
        }
    }
}
